package com.lz.ccjdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.lz.ccjdemo.R;

public class FlowPathStyleView extends LinearLayout {

    public FlowPathStyleView(Context context) {
        this(context, null);
    }

    public FlowPathStyleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public FlowPathStyleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private boolean isSelect = false;
    private int style = 0;
    private int defLineColor = Color.parseColor("#999999");
    private int selectLineColor = Color.parseColor("#6089F6");

    private int gravity = 0;

    private void init(Context context, AttributeSet attributeSet) {
        LayoutInflater.from(context).inflate(R.layout.item_flow_path_style, this);
        TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FlowPathStyleView, 0, 0);
        isSelect = attr.getBoolean(R.styleable.FlowPathStyleView_isSelect, false);
        style = attr.getInt(R.styleable.FlowPathStyleView_curStyle, 0);
        gravity = attr.getInt(R.styleable.FlowPathStyleView_gravity, 1);
        attr.recycle();

        View startLine = getLine();
        View dot = findViewById(R.id.dot);
        View endLine = findViewById(R.id.end_line);
        switch (style) {
            case 0:
                //start
                startLine.setVisibility(INVISIBLE);
                dot.setBackgroundResource(isSelect ? R.drawable.bg_dot_select : R.drawable.bg_dot_unselect);
                endLine.setBackgroundColor(isSelect ? selectLineColor : defLineColor);
                break;
            case 1:
                //cor
                startLine.setVisibility(VISIBLE);
                dot.setBackgroundResource(isSelect ? R.drawable.bg_dot_select : R.drawable.bg_dot_unselect);
                endLine.setBackgroundColor(isSelect ? selectLineColor : defLineColor);
                startLine.setBackgroundColor(isSelect ? selectLineColor : defLineColor);
                break;
            case 2:
                startLine.setBackgroundColor(isSelect ? selectLineColor : defLineColor);
                startLine.setVisibility(VISIBLE);
                dot.setBackgroundResource(isSelect ? R.drawable.bg_dot_select : R.drawable.bg_dot_unselect);
                endLine.setVisibility(INVISIBLE);
                break;
        }
    }

    private View getLine() {
        View balanceLine = findViewById(R.id.balance_line);
        View startLine = findViewById(R.id.start_line);
        switch (gravity) {
            case 0:
                //top
                balanceLine.setVisibility(GONE);
                return startLine;
            case 1:
                startLine.setVisibility(GONE);
                return balanceLine;
            default:
                balanceLine.setVisibility(GONE);
                return startLine;
        }
    }
}
